package hongcaosp.app.android.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import hongcaosp.app.android.R;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.TaskPool;

public class RegisterActivity extends BaseActivity implements ICodeView,IRegisterView {

    private RegisterPresenter registerPresenter;

    private EditText phoneET;
    private EditText codeET;
    private EditText inviteET;
    private TextView codeBTN;
    private TextView tv_login;

    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tv_login = findViewById(R.id.tv_login);
        tv_login.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PasswordLoginActivity.toLogin(RegisterActivity.this);
                finish();
            }
        });
        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        phoneET = findViewById(R.id.phone_et);
        codeET = findViewById(R.id.code_et);
        inviteET = findViewById(R.id.et_invite);
        codeBTN = findViewById(R.id.code_btn);

        codeBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codeAction();
            }
        });

        registerPresenter = new RegisterPresenter(this,this);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_register;
    }

    public static void toRegister(Context activity) {
        if (activity != null) {
            Intent intent = new Intent(activity,RegisterActivity.class);
            activity.startActivity(intent);
        }
    }

    private void register() {
        String phone = phoneET.getText().toString();
        String code = codeET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().showToast("请输入验证码");
            return;
        }
        registerPresenter.register(phone,code,inviteET.getText().toString().trim());
    }

    private void codeAction() {
        String phone = phoneET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        codeBTN.setEnabled(false);
        registerPresenter.sendCode(phone);
    }

    private void codeCount(int count) {
        final int n = count - 1;
        if (n >= 0) {
            codeBTN.setText(n + "S" );
            TaskPool.myPool().postTaskOnMainDelay(new Runnable() {
                @Override
                public void run() {
                    codeCount(n);
                }
            },1000);
        } else {
            codeBTN.setEnabled(true);
            codeBTN.setText("获取验证码");
        }
    }

    @Override
    public void onCodeSend(boolean success) {
        if (success) {
            codeCount(60);
        } else {
            codeBTN.setEnabled(true);
        }
    }

    @Override
    public void onRegister(boolean isOk, String message) {
        ToastManager.getInstance().showToast(message);
        if (isOk) {
            PasswordLoginActivity.toLogin(RegisterActivity.this);
        }
    }
}
